<?php

require_once("config.php");
require_once("util.php");
require_once("quizzes.php");
require_once("questions.php");
require_once("answers.php");
require_once("participants.php");

//echo $_SERVER['REQUEST_URI'];

REST_API_Mapping();

function REST_API_Mapping()
{
	$apiUri = GetAPIString($_SERVER['REQUEST_URI']);	
	$tmp = explode('?', $apiUri);
	$uriParts = explode('/', $tmp[0]);
	$uriPartsTrimmed[0] = "";
	
	for($i = 0; $i < count($uriParts); $i++)
	{
		if($i == count($uriParts) - 1 && $uriParts[$i] == "" && $i != 0)
		{
			break;
		}
		$uriPartsTrimmed[$i] = $uriParts[$i];
	}
	
	if(count($uriPartsTrimmed) == 1 && $uriPartsTrimmed[0] == "")
	{
		printErrorCode(404);
	}
	
	switch($uriPartsTrimmed[0])
	{
		case "quizzes":
			quizzes($uriPartsTrimmed, $_SERVER['REQUEST_METHOD']);
			break;
			
		case "meta":
			meta($uriPartsTrimmed, $_SERVER['REQUEST_METHOD']);
			break;
			
		default:
			printErrorCode(404);
			break;
	}
}

function meta($uriParts, $method)
{
	$count = count($uriParts);
	
	if($count == 1)
	{
		printErrorCode(404);
	}else
	{
		switch($uriParts[1])
		{
			default:
				printErrorCode(404);
				break;
		}
	}	
}

function quizzes($uriParts, $method)
{
	$count = count($uriParts);
	
	if($count == 1)
	{
		// /quizzes/
		if($method != "POST")
		{
			printErrorCode(405);
		}	
		
		$id = quizzes_POST();
		
		sendResponse(json_encode($id));
	}else
	{		
		// /quizzes/{id}/...
		if($count == 2)
		{
			if($method != "DELETE")
			{
				printErrorCode(405);
			}
			sendResponse(json_encode(quizzes_DELETE((int) $uriParts[1])));
		}
		
		switch($uriParts[2])
		{
			case "questions":
				questions($uriParts, $method);
				break;
			case "participants":
				participants($uriParts, $method);
				break;
		}		
	}
}

function participants($uriParts, $method)
{
	$count = count($uriParts);
	
	$id = (int) $uriParts[1];
	
	if($count == 3)
	{
		switch($method)
		{
			case "POST":
				$uid = participants_POST($id);
				sendResponse(json_encode($uid));				
				break;
			case "GET":
				$participants = participants_GET($id);
				sendResponse(json_encode($participants));
				break;
			default:
				printErrorCode(405);
				break;
		}
	}else 
	{
		$uid = (int) $uriParts[3];
		if($count == 4)
		{
			switch($method)
			{
				case "GET":
					$participant = participant_GET($id, $uid);
					sendResponse(json_encode($participant));					
					break;
				case "PUT":
					participant_PUT($id, $uid);
					sendResponse(json_encode(0));	
					break;
				case "DELETE":
					participant_DELETE($id, $uid);
					sendResponse(json_encode(0));	
					break;
				default:
					printErrorCode(405);
					break;
			}
		}else if($count == 5)
		{
			if($uriParts[4] != "answers")
			{
				printErrorCode(404);
			}
			if($method != "GET")
			{
				printErrorCode(405);
			}
			$answers = participants_answers_GET($id, $uid);
			sendResponse(json_encode($answers));
		}else
		{
			printErrorCode(404);
		}
	}
}

function questions($uriParts, $method)
{
	$count = count($uriParts);
	
	$id = (int) $uriParts[1];
	if($count == 3)
	{
		// /quizzes/{id}/questions
		
		switch($method)
		{
			case "POST":
				$result = questions_POST($id);
				sendResponse(json_encode($result));
				break;
			case "GET":
				$questionData = questions_GET($id);
				sendResponse(json_encode($questionData));
				break;
			case "PUT":
				$result = questions_PUT($id);
				sendResponse(json_encode($result));
			default:
				printErrorCode(405);
				break;
		}
	}else
	{
		// /quizzes/{id}/questions/...
		$qid = (int) $uriParts[3];
		if($count == 4)
		{
			// /quizzes/{id}/questions/{qid}
			switch($method)
			{
				case "GET":
					$result = question_GET($id, $qid);
					sendResponse(json_encode($result));
					break;
				case "PUT":
					$result = question_PUT($id, $qid);
					sendResponse(json_encode($result));					
					break;
				case "DELETE":
					$result = question_DELETE($id, $qid);
					sendResponse(json_encode($result));
					break;
				default:
					printErrorCode(405);
					break;
			}
		}else if($count == 5 || $count == 6)
		{
			if($uriParts[4] != "answers")
			{
				printErrorCode(404);
			}
			answers($uriParts, $method);
		}else
		{
			printErrorCode(404);
		}
	}
}

function answers($uriParts, $method)
{
	$count = count($uriParts);
	
	$id = (int) $uriParts[1];
	$qid = (int) $uriParts[3];
	
	if($count == 5)
	{
		switch($method)
		{
			case "POST":
				$result = answers_POST($id, $qid);
				sendResponse(json_encode($result));
				break;
			case "GET":
				$answers = answers_GET($id, $qid);
				sendResponse(json_encode($answers));
				break;
			case "DELETE":
				$result = answers_DELETE($id, $qid);
				sendResponse(json_encode($result));
				break;
			default:
				printErrorCode(405);
				break;
		}
	}else if($count == 6)
	{
		$uid = (int) $uriParts[5];
		switch($method)
		{
			case "PUT":
				answer_PUT($id, $qid, $uid);
				break;
			case "GET":
				answer_GET($id, $qid, $uid);
				break;
			case "DELETE":
				answer_DELETE($id, $qid, $uid);
				break;
			default:
				printErrorCode(405);
				break;
		}
	}
}

function GetAPIString($requestURI)
{
	global $api_uri;
	$length = strlen($api_uri);
	$requestSubstr = substr($requestURI, 0, $length);
	
	if(strtolower($requestSubstr) != strtolower($api_uri))
	{		
		printErrorCode(500);
	}
	
	return substr($requestURI, strlen($api_uri));
}

?>